package com.lingxu.module.result.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.system.fileManage.mapper.SysFileInfoMapper;
import com.lingxu.module.message.entity.UserMessage;
import com.lingxu.module.message.mapper.MessageMapper;
import com.lingxu.module.result.entity.ApprovalParams;
import com.lingxu.module.result.entity.ModelFeed;
import com.lingxu.module.result.entity.ResultFeedback;
import com.lingxu.module.result.mapper.ResultFeedbackMapper;
import com.lingxu.module.result.service.ResultFeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ResultFeedbackServiceImpl
 * @Package: com.lingxu.module.result.service.impl
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2023/12/7 9:17
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@Service
public class ResultFeedbackServiceImpl extends ServiceImpl<ResultFeedbackMapper, ResultFeedback>
        implements ResultFeedbackService {

    @Autowired
    private ResultFeedbackMapper resultFeedbackMapper;
    @Autowired
    private SysFileInfoMapper sysFileInfoMapper;
    @Autowired
    private MessageMapper messageMapper;

    /**
     * @Author ysj闫世杰
     * @Description 查询需要审批成效反馈记录的模型
     * @Date 2023/12/7 9:32
     * @Param: page
     * @Param: limit
     * @Param: status
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result findModelFeedBackList(Integer page, Integer limit, String status,String keyWord) {
        try {
            Page<ModelFeed> feedPage = new Page<>(page,limit);
            IPage<ModelFeed> list = resultFeedbackMapper.findModelFeedBackList(feedPage,status,keyWord);
            return Result.ok(list,"查询成功");
        }catch (Exception e){
            log.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }


    /**
     * @Author ysj闫世杰
     * @Description 查询待审批的成效反馈列表
     * @Date 2023/12/7 11:04
     * @Param: page
     * @Param: limit
     * @Param: status
     * @Param: modelId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result findNeedApproval(Integer page, Integer limit,String status,String modelId,String types) {
        try {
            Page<ResultFeedback> feedbackPage = new Page<>(page,limit);
            IPage<ResultFeedback> list = resultFeedbackMapper.findNeedApproval(feedbackPage,status,modelId,types);
            return Result.ok(list,"查询成功");
        }catch (Exception e){
            log.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }


    @Override
    public Result findupLoadFileList(String id) {
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("RECORD_ID",id);
            return Result.ok(sysFileInfoMapper.selectByMap(map),"查询成功");
        }catch (Exception e){
            log.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }

    /**
     * @Author ysj闫世杰
     * @Description 审批
     * @Date 2023/12/7 16:07
     * @Param: ids
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result updateApproval(ApprovalParams params) {
        try {
            int i = resultFeedbackMapper.updateApproval(params);
            String content = "";
            if(i > 0){
                //同意
                if("1".equals(params.getApprovalStatus())){
                    content = "您填写的模型成效反馈已审批通过!";
                } else {
                    content = "您填写的模型成效反馈审批未通过!";
                }
                UserMessage message = new UserMessage();
                message.setIsRead("0");
                message.setContent(content);
                message.setUserId(params.getCreateUser());
                messageMapper.insert(message);
            }
            return Result.ok("操作成功");
        }catch (Exception e){
            log.error("操作异常",e);
            return Result.error("操作异常");
        }
    }
}
